package com.example.jpatest.controller.restful;

import com.example.jpatest.entity.Student;
import com.example.jpatest.entity.Team;
import com.example.jpatest.exception.teamException.HasInTeamException;
import com.example.jpatest.exception.teamException.NotInTeamException;
import com.example.jpatest.exception.studentException.StudentExistException;
import com.example.jpatest.exception.studentException.StudentNotExistException;
import com.example.jpatest.exception.teamException.TeamNotExistException;
import com.example.jpatest.service.impl.StudentServiceImpl;
import com.example.jpatest.service.impl.TeamServiceImpl;
import com.example.jpatest.unit.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生Controller
 * @author czx
 */

@RestController
public class StudentController {
    @Autowired
    StudentServiceImpl studentService;

    @Autowired
    TeamServiceImpl teamService;

    /**
     * 获得所有学生
     */
    @GetMapping("/students")
    public List<Student> students(){
        return studentService.getStudents();
    }

    /**
     * 添加学生
     */
    @PostMapping("/student")
    public Result addStudent(@RequestBody Student student){
       if(studentService.getByStudentNumber(student.getStudentNumber())!=null){
           throw new StudentExistException();
       }
       studentService.saveStudent(student);
       return Result.succMsg();
    }

    /**
     * 更新学生
     */
    @PutMapping("/student")
    public Result updateStudent(@RequestBody Student student){
        Student byStudentNumber = studentService.getByStudentNumber(student.getStudentNumber());
        if(byStudentNumber ==null){
            throw new StudentNotExistException();
        }
        student.setId(byStudentNumber.getId());
        studentService.saveStudent(student);
        return Result.succMsg();
    }

    /**
     * 根据学号查找学生
     */
    @GetMapping("/student/{studentNumber}")
    public Result getStudent(@PathVariable String studentNumber){
        Student student = studentService.getByStudentNumber(studentNumber);
        if(student==null){
            throw new StudentNotExistException();
        }
        return Result.succ(student);
    }

    /**
     * 学生加入队伍
     */
    @PostMapping("/student/{studentNumber}/team")
    public Result studentTeams(
            @PathVariable String studentNumber,
            @RequestParam(name = "teamName") String teamName) {
        Student student = studentService.getByStudentNumber(studentNumber);
        Team team = teamService.getByName(teamName);
        if (student == null) {
            throw new StudentNotExistException();
        }
        if (team == null) {
            throw new TeamNotExistException();
        }
        if (studentService.isInTeam(student,team)) {
            throw new HasInTeamException();
        }
        studentService.joinTeam(student,team);
        return Result.succMsg();
    }

    /**
     * 查找学生所有队伍
     */
    @GetMapping("/student/{studentNumber}/teams")
    public Result getTeams(@PathVariable String studentNumber){
        Student student = studentService.getByStudentNumber(studentNumber);
        if (student == null) {
            throw new StudentNotExistException();
        }
        return Result.succ(studentService.getAllTeams(student));

    }

    /**
     * 根据学号删除学生
     */
    @DeleteMapping("/student/{studentNumber}")
    public Result deleteStudent(@PathVariable String studentNumber){
        if(studentService.getByStudentNumber(studentNumber)==null){
            throw new StudentNotExistException();
        }
        studentService.deleteByStudentNumber(studentNumber);
        return Result.succMsg();
    }

    /**
     * 学生退出队伍
     */
    @DeleteMapping("/student/{studentNumber}/team")
    public Result quitTeam(
            @PathVariable String studentNumber,
            @RequestParam(name = "teamName") String teamName){
        Student student = studentService.getByStudentNumber(studentNumber);
        Team team = teamService.getByName(teamName);
        if (student == null) {
            throw new StudentNotExistException();
        }
        if (team == null) {
            throw new TeamNotExistException();
        }
        if (!student.getTeams().contains(team)) {
            throw new NotInTeamException();
        }
        studentService.quitTeam(student,team);
        studentService.saveStudent(student);
        return Result.succMsg();
    }

    /**
     * 查找学生报名
     */
    @GetMapping("/student/{studentNumber}/enters")
    public Result getEnters(@PathVariable String studentNumber){
        Student student = studentService.getByStudentNumber(studentNumber);
        if (student == null) {
            throw new StudentNotExistException();
        }
        return Result.succ(studentService.getEnters(student));
    }
}
